package com.ruoyi.system.dubbo;

import cn.hutool.core.convert.Convert;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.ruoyi.system.api.RemoteAssignerService;
import com.ruoyi.system.api.domain.SysDept;
import com.ruoyi.system.api.domain.SysRole;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.domain.TaskAssignee;
import com.ruoyi.system.api.model.TaskAssigneeDTO;
import com.ruoyi.system.domain.SysPost;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysPostService;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.service.ISysUserService;
import lombok.RequiredArgsConstructor;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 工作流设计器获取任务执行人
 *
 * @author Lion Li
 */
@RequiredArgsConstructor
@Service
@DubboService
public class RemoteAssignerServiceImpl implements RemoteAssignerService {

    // 上级Service注入下级Service 其他Service永远不可能注入当前类 避免循环注入
    private final ISysPostService postService;
    private final ISysDeptService deptService;
    private final ISysUserService userService;
    private final ISysRoleService roleService;

    /**
     * 查询角色并返回任务指派的列表，支持分页
     *
     * @param taskQuery 查询条件
     * @return 办理人
     */
    @Override
    public TaskAssignee selectRolesByTaskAssigneeList(TaskAssigneeDTO taskQuery) {
        PageQuery pageQuery = new PageQuery(taskQuery.getPageSize(), taskQuery.getPageNum());
        SysRole bo = new SysRole();
        bo.setRoleKey(taskQuery.getHandlerCode());
        bo.setRoleName(taskQuery.getHandlerName());
        Map<String, Object> params = bo.getParams();
        params.put("beginTime", taskQuery.getBeginTime());
        params.put("endTime", taskQuery.getEndTime());
        TableDataInfo<SysRole> page = roleService.selectPageRoleList(bo, pageQuery);
        // 使用封装的字段映射方法进行转换
        List<TaskAssignee.TaskHandler> handlers = TaskAssignee.convertToHandlerList(page.getRows(),
            SysRole::getRoleId, SysRole::getRoleKey, SysRole::getRoleName, null, SysRole::getCreateTime);
        return new TaskAssignee(page.getTotal(), handlers);
    }

//    /**
//     * 查询部门并返回任务指派的列表，支持分页
//     *
//     * @param taskQuery 查询条件
//     * @return 办理人
//     */
//    @Override
//    public TaskAssignee selectDeptsByTaskAssigneeList(TaskAssigneeDTO taskQuery) {
//        PageQuery pageQuery = new PageQuery(taskQuery.getPageSize(), taskQuery.getPageNum());
//        SysDept bo = new SysDept();
//        bo.setDeptCategory(taskQuery.getHandlerCode());
//        bo.setDeptName(taskQuery.getHandlerName());
//        Map<String, Object> params = bo.getParams();
//        params.put("beginTime", taskQuery.getBeginTime());
//        params.put("endTime", taskQuery.getEndTime());
//        bo.setBelongDeptId(Convert.toLong(taskQuery.getGroupId()));
//        TableDataInfo<SysDeptVo> page = deptService.selectPageDeptList(bo, pageQuery);
//        // 使用封装的字段映射方法进行转换
//        List<TaskAssignee.TaskHandler> handlers = TaskAssignee.convertToHandlerList(page.getRows(),
//            SysDeptVo::getDeptId, SysDeptVo::getDeptCategory, SysDeptVo::getDeptName, SysDeptVo::getParentId, SysDeptVo::getCreateTime);
//        return new TaskAssignee(page.getTotal(), handlers);
//    }


    /**
     * 查询用户并返回任务指派的列表，支持分页
     *
     * @param taskQuery 查询条件
     * @return 办理人
     */
    @Override
    public TaskAssignee selectUsersByTaskAssigneeList(TaskAssigneeDTO taskQuery) {
        PageQuery pageQuery = new PageQuery(taskQuery.getPageSize(), taskQuery.getPageNum());
        SysUser bo = new SysUser();
        bo.setUserName(taskQuery.getHandlerCode());
        bo.setNickName(taskQuery.getHandlerName());
        Map<String, Object> params = bo.getParams();
        params.put("beginTime", taskQuery.getBeginTime());
        params.put("endTime", taskQuery.getEndTime());
        bo.setDeptId(Convert.toLong(taskQuery.getGroupId()));
        TableDataInfo<SysUser> page = userService.selectPageUserList(bo, pageQuery);
        // 使用封装的字段映射方法进行转换
        List<TaskAssignee.TaskHandler> handlers = TaskAssignee.convertToHandlerList(page.getRows(),
            SysUser::getUserId, SysUser::getUserName, SysUser::getNickName, SysUser::getDeptId, SysUser::getCreateTime);
        return new TaskAssignee(page.getTotal(), handlers);
    }

}
